home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pcmagazi / 1989 / 02 / ansi.asm next >
Assembly Source File  |  1988-10-27  |  64KB  |  1,236 lines

  1. ;------------------------------------------------------------------------;
  2. ;  ANSI.COM - Replacement for the ANSI.SYS console device driver.        ;
  3. ;  Unlike ANSI.SYS which must be installed at boot time, ANSI.COM        ;
  4. ;  can be installed and uninstalled at anytime.  Enhancements include    ;
  5. ;  a fast screen write and variable sized keyboard reassignment buffer.  ;
  6. ;------------------------------------------------------------------------;
  7. _TEXT          SEGMENT PUBLIC 'CODE'
  8.                ASSUME  CS:_TEXT,DS:_TEXT,ES:_TEXT,SS:_TEXT
  9.                ORG     100H
  10. START:         JMP     INITIALIZE
  11.  
  12. SIGNATURE      DB      "ANSI 1.0 (C) 1989 Ziff Communications Co.",CR,LF
  13. PROGRAMMER     DB      "PC Magazine ",254," Michael J. Mefford",CR,LF,LF,"$",26
  14.  
  15. CR             EQU     13
  16. LF             EQU     10
  17. SPACE          EQU     32
  18. ESC_CHAR       EQU     27
  19. SINGLE_QUOTE   EQU     39
  20. DOUBLE_QUOTE   EQU     34
  21. BELL           EQU     7
  22. BS             EQU     8
  23.  
  24. OFF            EQU     1
  25. ON             EQU     2
  26. SLOW           EQU     4
  27. FAST           EQU     8
  28. STATUS_MASK    EQU     11111100B
  29.  
  30. ANSI_STATE     DW      ESC_STATE
  31. STATUS         DB      ON OR FAST
  32. PARAMETERS     DB      "OFF ON  SLOWFAST"
  33. LAST_PARAMETER EQU     $ - PARAMETERS
  34.  
  35. OLD_INT_29     DW      ?,?
  36. OLD_INT_16     DW      ?,?
  37. OLD_INT_21     DW      ?,?
  38.  
  39. ATTRIBUTE      DB      7
  40. SAVE_POSITION  DW      0
  41. LINE_WRAP      DB      ON
  42. QUOTE_TYPE     DB      ?
  43. ESC_COUNT      DW      0
  44. NUMBER_COUNT   DW      0
  45.  
  46. FORMAT_CHARS   DB      CR,LF,BS,BELL
  47. FORMAT_LENGTH  EQU     $ - FORMAT_CHARS
  48.  
  49. COMMAND_TABLE  LABEL   BYTE
  50. DB   "H", "A", "B", "C", "D", "f", "n", "s", "u", "K", "m", "h", "l", "p", "J"
  51. COMMAND_LENGTH EQU     $ - COMMAND_TABLE
  52.  
  53. DW   CURS_POSITION, CURSOR_UP,     CURSOR_DOWN, CURS_FORWARD, CURS_BACKWARD
  54. DW   HORZ_VERT_POS, DEVICE_STATUS, SAVE_CURSOR, RESTORE_CURS, ERASE_IN_LINE
  55. DW   SGR,           SET_MODE,      RESET_MODE,  REASSIGNMENT, CLS
  56. COMMAND_END    EQU     $ - 2
  57.  
  58. ATTRIBUTE_TABLE        LABEL    BYTE
  59. DB   00,01,04,05,07,08,30,31,32,33,34,35,36,37,40,41,42,43,44,45,46,47
  60. ATTRIBUTE_LENGTH       EQU      $ - ATTRIBUTE_TABLE
  61.  
  62. ;Format: AND mask,OR mask
  63. DB      000H,07H, 0FFH,08H, 0F8H,01H, 0FFH,80H, 0F8H,70H, 088H,00H
  64. DB      0F8H,00H, 0F8H,04H, 0F8H,02H, 0F8H,06H, 0F8H,01H, 0F8H,05H
  65. DB      0F8H,03H, 0F8H,07H, 08FH,00H, 08FH,40H, 08FH,20H, 08FH,60H
  66. DB      08FH,10H, 08FH,50H, 08FH,30H, 08FH,70H
  67. ATTRIBUTE_END          EQU      $ - 2
  68.  
  69. BIOS_ACTIVE_PAGE       EQU     62H
  70. ACTIVE_PAGE    DB      ?
  71. ADDR_6845      DW      ?
  72. BIOS_CRT_MODE          EQU     49H
  73. CRT_MODE       DB      ?
  74. CRT_COLS       DW      ?
  75. CRT_LEN        DW      ?
  76. CRT_START      DW      ?
  77. CRT_DATA_LENGTH        EQU     $ - CRT_MODE
  78. CURSOR_POSN    LABEL   WORD
  79. CURSOR_COL     DB      ?
  80. CURSOR_ROW     DB      ?
  81. CRT_ROWS       DB      ?
  82.  
  83. DOS_INPUT      DB      OFF
  84. BUSY_FLAG      DB      OFF
  85. REASSIGN_FLAG  DB      OFF
  86. REMOVE_FLAG    DB      ON
  87. REASSIGN_COUNT DW      0
  88. REASSIGN_POS   DW      ?
  89. FUNCTION_16    DB      ?
  90. ZR             EQU     1000000B
  91.  
  92. ESC_BUFFER_SIZE      EQU     126
  93. REASSIGNMENT_DEFAULT EQU     200
  94. REASSIGNMENT_SIZE    DW      REASSIGNMENT_DEFAULT
  95. REASSIGNMENT_MAX     EQU     60 * 1024
  96. REASSIGN_END         DW      REASSIGNMENT_BUFFER
  97. BUFFER_END           DW      REASSIGNMENT_BUFFER + REASSIGNMENT_DEFAULT
  98.  
  99. ;------------------------------------------------------------------------------;
  100. ; INT 29 is an undocumented interrupt called by DOS for output to the console. ;
  101. ;------------------------------------------------------------------------------;
  102. ANSI_INT_29    PROC    FAR
  103.  
  104.                PUSH    AX                      ;Save all registers.
  105.                PUSH    BX
  106.                PUSH    CX
  107.                PUSH    DX
  108.                PUSH    SI
  109.                PUSH    DI
  110.                PUSH    DS
  111.                PUSH    ES
  112.                PUSH    BP
  113.  
  114.                CLD                             ;All string operations forward.
  115.                MOV     BX,CS                   ;Point to our data segment.
  116.                MOV     DS,BX
  117.                MOV     ES,BX
  118.                CALL    ANSI_STATE              ;Call the current state of
  119.                                                ; the ANSI Esc sequence.
  120.                POP     BP
  121.                POP     ES
  122.                POP     DS
  123.                POP     DI
  124.                POP     SI
  125.                POP     DX
  126.                POP     CX
  127.                POP     BX
  128.                POP     AX                      ;Restore all registers and
  129.                IRET                            ; return.
  130. ANSI_INT_29    ENDP
  131.  
  132. ;------------------------------------------------;
  133. ANSI_INT_21    PROC    FAR
  134.                PUSHF
  135.                CMP     AH,0AH                  ;If DOS Int 21 functions Ah,
  136.                JZ      INPUT                   ; 7h, 1h or console input (6h)
  137.                CMP     AH,7                    ; then tell INT 16, DOS input
  138.                JZ      INPUT                   ; is active.
  139.                CMP     AH,1
  140.                JZ      INPUT
  141.                CMP     AH,6
  142.                JNZ     QUICK21_EXIT
  143.                CMP     DL,0FFH
  144.                JZ      INPUT
  145. QUICK21_EXIT:  POPF                              ;If not, let the original
  146.                JMP     DWORD PTR CS:OLD_INT_21   ; interrupt process the call.
  147.  
  148. INPUT:         POPF
  149.                MOV     CS:DOS_INPUT,ON           ;INT 16 handler flag.
  150.                PUSHF
  151.                CALL    DWORD PTR CS:OLD_INT_21   ;Emulate an interrupt.
  152.                MOV     CS:DOS_INPUT,OFF          ;Turn flag back off.
  153.                RET     2                         ;Return with current flags.
  154. ANSI_INT_21    ENDP
  155.  
  156. ;-----------------------------------------------------------------;
  157. ; If we get here via a DOS console input call, any awaiting key   ;
  158. ; in keyboard buffer is checked to see if it is to be reassigned. :
  159. ;-----------------------------------------------------------------;
  160. ANSI_INT_16    PROC    FAR
  161.                STI                             ;Interrupts back on.
  162.                PUSHF                           ;Preserve flags on stack.
  163.                PUSH    BP
  164.                PUSH    DS
  165.                PUSH    AX
  166.                PUSH    CS                      ;Point to our data.
  167.                POP     DS
  168.  
  169.                CMP     DOS_INPUT,OFF           ;If not called via a DOS
  170.                JZ      QUICK16_EXIT            ; console input call, exit.
  171.                CMP     BUSY_FLAG,ON            ;If already processing a call,
  172.                JZ      QUICK16_EXIT            ; exit.
  173.                CLD                             ;All string operations forward.
  174.                MOV     BP,SP                   ;Base reference to flags on stack
  175.                MOV     FUNCTION_16,AH          ;Save function request.
  176.                AND     AH,NOT 10H              ;Strip possible extended request.
  177.                JZ      GET_KEY                 ;If zero, it's wait for a key.
  178.                DEC     AH                      ;Else, decrement.  If zero it's
  179.                JZ      KEY_STATUS              ; key status, else exit.
  180.  
  181. QUICK16_EXIT:  POP     AX                      ;Restore registers and let
  182.                POP     DS                      ; original interrupt handler
  183.                POP     BP                      ; process the call.
  184.                POPF
  185.                JMP     DWORD PTR CS:OLD_INT_16
  186.  
  187. KEY_STATUS:    OR      BYTE PTR [BP+6],ZR      ;Assume none ready; ZR = 1.
  188. GET_KEY:       MOV     BUSY_FLAG,ON            ;Non-reentrant; flag busy.
  189.                POP     AX
  190.                PUSH    BX                      ;Save some more registers.
  191.                PUSH    CX
  192.                PUSH    DX
  193.                PUSH    SI
  194.                PUSH    DI
  195.                CMP     REASSIGN_FLAG,ON        ;Already in process of reassign?
  196.                JZ      CK_BUFFER               ;If yes, check kbd buffer.
  197.  
  198.                PUSHF                           ;Else, emulate an interrupt.
  199.                CALL    DWORD PTR OLD_INT_16
  200.                PUSHF                           ;Status call results in flags.
  201.                TEST    FUNCTION_16,1           ;Was it a status call?
  202.                JZ      CK_DUP                  ;If no, check key returned.
  203.                POPF                            ;Else, retrieve status results.
  204.                JZ      INT16_EXIT              ;If zero, no key waiting.
  205.                AND     BYTE PTR [BP+6],NOT ZR  ;Else, ZR = 0.
  206.                PUSHF                           ;PUSHF just to keep stack right.
  207.  
  208. CK_DUP:        POPF                            ;Fix stack.
  209.                MOV     BX,AX                   ;Match procedure wants key in BX.
  210.                CALL    CK_MATCH                ;Check reassignment buffer.
  211.                JC      INT16_EXIT              ;If no match, exit.
  212.                MOV     AX,[DI]                 ;Else, retrieve string length.
  213.                SUB     AX,3                    ;Adjust for length word and match
  214.                ADD     DI,3                    ; byte; same for string pointer.
  215.                OR      BL,BL                   ;Extended key? ie. key = 0.
  216.                JNZ     STORE_COUNT             ;If no, save length and pointer.
  217.                DEC     AX                      ;Else, adjust for extended code.
  218.                INC     DI
  219. STORE_COUNT:   MOV     REASSIGN_COUNT,AX       ;Save string length.
  220.                MOV     REASSIGN_POS,DI         ;Save pointer to string.
  221.                MOV     REASSIGN_FLAG,ON        ;Flag that replacement in effect.
  222. CK_BUFFER:     TEST    FUNCTION_16,1           ;Was it a key wait function call?
  223.                JNZ     RETRIEVE                ;If no, key status; get it.
  224.                CMP     REMOVE_FLAG,OFF         ;Removed it from kbd buffer?
  225.                JZ      RETRIEVE                ;If yes, get reassignment.
  226.                MOV     AH,FUNCTION_16          ;Else, eat the replaced character
  227.                INT     16H                     ; via INT 16.
  228.                MOV     REMOVE_FLAG,OFF         ;Flag character eaten.
  229.  
  230. RETRIEVE:      MOV     DI,REASSIGN_POS         ;Retrieve pointer to string.
  231.                MOV     AX,[DI]                 ;Retrieve replacement character.
  232.                TEST    FUNCTION_16,1           ;Is it a key wait function call?
  233.                JZ      REMOVE                  ;If yes, bump pointer up one.
  234.                AND     BYTE PTR [BP+6],NOT ZR  ;If no, status call; ZR = 0
  235.                JMP     SHORT INT16_EXIT        ;All done.
  236.  
  237. REMOVE:        INC     REASSIGN_POS            ;Move pointer to next character.
  238.                DEC     REASSIGN_COUNT          ;One less character to replace.
  239.                JZ      REASSIGN_DONE           ;If end of string, reset flags.
  240.                OR      AL,AL                   ;Else, extended replacement?
  241.                JNZ     INT16_EXIT              ;If no, done here.
  242.                INC     REASSIGN_POS            ;Else adjust pointers.
  243.                DEC     REASSIGN_COUNT
  244.                JNZ     INT16_EXIT              ;End of string?
  245. REASSIGN_DONE: MOV     REASSIGN_FLAG,OFF       ;If yes, reset flags.
  246.                MOV     REMOVE_FLAG,ON
  247.  
  248. INT16_EXIT:    MOV     BUSY_FLAG,OFF           ;Reset the busy flag.
  249.                POP     DI                      ;Restore registers.
  250.                POP     SI
  251.                POP     DX
  252.                POP     CX
  253.                POP     BX
  254.                POP     DS
  255.                POP     BP
  256.                POPF                            ;Flags on stack have status
  257.                RET     2                       ; results; kill old flags.
  258. ANSI_INT_16    ENDP
  259.  
  260. ;************* ANSI ESCAPE STATE ROUTINES ************* ;
  261.  
  262. ESC_STATE:     MOV     BX,OFFSET BRACKET_STATE ;Assume Esc character.
  263.                CMP     AL,ESC_CHAR             ;Is it Esc?  If yes, store
  264.                JZ      SHORT_JUMP              ; char. and next state.
  265.                JMP     WRITE_CHAR              ;Else, normal char; write it.
  266.  
  267. ;------------------------------------------------;
  268. BRACKET_STATE: MOV     BX,OFFSET SPECIAL_STATE ;Assume left bracket.
  269.                CMP     AL,"["                  ;Is it left bracket?  If yes,
  270. SHORT_JUMP:    JZ      STORE_STATE             ;store char. and next state.
  271.                JMP     FLUSH_BUFFER            ;Else, flush Esc out of buffer.
  272.  
  273. ;---------------------------------------------------------------;
  274. ; Must process <ESC>[2J (CLS) regardless if ANSI.COM ON or OFF. ;
  275. ;---------------------------------------------------------------;
  276. SPECIAL_STATE: MOV     BX,OFFSET CLS_STATE     ;Assume Erase in Display code.
  277.                CMP     AL,"2"                  ;Is it the "2" ?
  278.                JZ      STORE_STATE             ;If yes, progress to next state.
  279.                TEST    STATUS,OFF              ;Else, is ANSI OFF ?
  280.                JNZ     FLUSH_BUFFER            ;If yes, flush Esc, bracket.
  281.                MOV     BX,OFFSET PARAM_STATE   ;Else, check for first Set,
  282.                CMP     AL,"="                  ; Reset Mode characters of
  283.                JZ      STORE_STATE             ; "=" and "?".
  284.                CMP     AL,"?"
  285.                JZ      STORE_STATE             ;If found, store.
  286.                JMP     SHORT DO_PARAMETER      ;Else, check other codes.
  287.  
  288. ;------------------------------------------------;
  289. CLS_STATE:     CMP     AL,"J"                  ;Is it second character of CLS?
  290.                MOV     DI,OFFSET COMMAND_END   ;If yes, execute
  291.                JZ      EXECUTE                 ; Erase in Display procedure.
  292.                TEST    STATUS,OFF              ;ANSI OFF?  If yes, flush buffer.
  293.                JNZ     FLUSH_BUFFER            ; If not fall through to process.
  294.                MOV     BYTE PTR NUMBER_BUFFER,2   ;Store the previous 2.
  295. DO_PARAMETER:  MOV     ANSI_STATE,OFFSET PARAM_STATE  ;Parameter state.
  296.  
  297. ;------------------------------------------------;
  298. PARAM_STATE:   CALL    CK_QUOTE                ;Is it single or double quotes?
  299.                JZ      STORE_STATE             ;If yes, store string state.
  300.                CMP     AL,";"                  ;Is it semi-colon delimiter?
  301.                JNZ     CK_NUMBER               ;If no, check if number.
  302.                INC     NUMBER_COUNT            ;Else, increment number count.
  303.                JMP     SHORT BUFFER_CHAR       ;Buffer the semi-colon.
  304.  
  305. CK_NUMBER:     CMP     AL,"0"                  ;Is it below 0 ?
  306.                JB      FLUSH_BUFFER            ;If yes, illegal; flush buffer.
  307.                CMP     AL,"9"                  ;Else, is it above 9 ?
  308.                JA      DO_COMMAND              ;If yes, check for command char.
  309.                CALL    ACCUMULATE              ;Else it's a number; accumulate.
  310.                JMP     SHORT BUFFER_CHAR       ;Buffer the character.
  311.  
  312. DO_COMMAND:    MOV     DI,OFFSET COMMAND_TABLE ;Point to legal ANSI commands.
  313.                MOV     CX,COMMAND_LENGTH       ;Number of commands.
  314.                REPNZ   SCASB                   ;Check for a match.
  315.                JNZ     FLUSH_BUFFER            ;If no match, flush sequence.
  316.                INC     NUMBER_COUNT            ;Else, increment for last number.
  317.                MOV     DI,OFFSET COMMAND_END   ;Point to appropriate command
  318.                SHL     CX,1                    ; processing procedure.
  319.                SUB     DI,CX
  320. EXECUTE:       CALL    GET_BIOS_DATA           ;Get cursor position data.
  321.                CALL    DS:[DI]                 ;Do command subroutine.
  322.                JMP     SHORT FLUSH_END         ;Clear buffer.
  323.  
  324. ;------------------------------------------------;
  325. QUOTE_STATE:   CMP     AL,QUOTE_TYPE           ;Is it an ending string quote?
  326.                JNZ     BUFFER_CHAR             ;If no, buffer literal.
  327.                MOV     BX,OFFSET PARAM_STATE   ;Else, back to parameter parsing.
  328.  
  329. ;------------------------------------------------;
  330. STORE_STATE:   MOV     ANSI_STATE,BX           ;Store the ANSI state.
  331.  
  332. ;------------------------------------------------;
  333. BUFFER_CHAR:   MOV     DI,ESC_COUNT            ;Buffer the character in case
  334.                CMP     DI,ESC_BUFFER_SIZE      ; ending ANSI command is illegal.
  335.                JZ      FLUSH_BUFFER            ;If buffer full, flush.
  336.                ADD     DI,OFFSET ESC_BUFFER    ;Point to next buffer position.
  337.                STOSB                           ;Store the character.
  338.                INC     ESC_COUNT               ;Increment the sequence count.
  339.                RET
  340.  
  341. ;------------------------------------------------;
  342. FLUSH_BUFFER:  PUSH    AX                      ;Save the current character.
  343.                MOV     SI,OFFSET ESC_BUFFER    ;Point to the sequence buffer.
  344.                MOV     CX,ESC_COUNT            ;Count of buffered characters.
  345. NEXT_FLUSH:    LODSB                           ;Retrieve one.
  346.                PUSH    CX                      ;Save counter and pointer.
  347.                PUSH    SI
  348.                CALL    WRITE_CHAR              ;Write character to screen.
  349.                POP     SI                      ;Restore counter and pointer.
  350.                POP     CX
  351.                LOOP    NEXT_FLUSH              ;Flush entire buffer.
  352.                POP     AX                      ;Retrieve last character.
  353.                CALL    WRITE_CHAR              ;Write it also.
  354. FLUSH_END:     MOV     ESC_COUNT,0                   ;Reset counter.
  355.                MOV     ANSI_STATE,OFFSET ESC_STATE   ;Back to Esc state.
  356.                MOV     NUMBER_COUNT,0                ;Reset parameter counter.
  357.                PUSH    CS                            ;Pointábbbbbb■bbbbbbb■bbbbábbbbbbbbbbbbbbbbábbbbbbbbbbbábbbbábbbbbbbábbbbbOV     CX,FORMAT_LENGTH        ; return, linefeed, backspace
  358.                REPNZ   SCASB                   ; and bell via
  359.                JZ      WRITE_TTY               ; TTY.
  360.  
  361.                CALL    GET_BIOS_DATA           ;Get BIOS video data.
  362.                CMP     AL,9                    ;Is character a TAB?
  363.                JNZ     CK_ACTIVE               ;If no, process normally.
  364.                MOV     CX,CURSOR_POSN          ;Else, expand TAB to
  365.                AND     CX,7                    ; appropriate space characters.
  366.                NEG     CX
  367.                ADD     CX,8
  368. NEXT_TAB:      PUSH    CX
  369.                MOV     AL,SPACE
  370.                CALL    CK_ACTIVE
  371.                POP     CX
  372.                LOOP    NEXT_TAB
  373.                RET
  374.  
  375. CK_ACTIVE:     TEST    STATUS,OFF              ;Is ANSI OFF?
  376.                JNZ     WRITE_TTY               ;If yes, write via BIOS TTY.
  377. CK_FAST:       CALL    CK_SLOW_TEXT            ;Is ANSI SLOW or in graphics
  378.                JNC     WRITE_FAST              ; mode? If no, write fast.
  379.  
  380. WRITE_SLOW:    PUSH    AX                      ;Else, write character/attribute
  381.                MOV     CX,1                    ; at current cursor position
  382.                MOV     BH,ACTIVE_PAGE          ; via BIOS.
  383.                MOV     BL,ATTRIBUTE
  384.                MOV     AH,9
  385.                INT     10H
  386.                POP     AX
  387.  
  388.                CMP     LINE_WRAP,ON            ;Is line wrap on?
  389.                JZ      TTY                     ;If yes, continue.
  390.                MOV     CX,CRT_COLS             ;Else, cursor at rightmost
  391.                DEC     CL                      ; column?
  392.                CMP     CL,CURSOR_COL           ;If yes, continue, else
  393.                JNZ     TTY                     ; return without writing.
  394.                RET
  395.  
  396. ;------------------------------------------------;
  397. WRITE_TTY:     MOV     BL,7                    ;Attribute in graphics mode.
  398. TTY:           MOV     AH,0EH
  399.                INT     10H
  400.                RET
  401.  
  402. ;----------------------------------------------------------------------------;
  403. ; Fast screen writes are directly to the video buffer without retrace check. ;
  404. ;----------------------------------------------------------------------------;
  405. WRITE_FAST:    PUSH    ES                      ;Preserve extra segment.
  406.                MOV     DX,CURSOR_POSN          ;Retrieve cursor position.
  407.                CALL    VIDEO_SETUP             ;Calculate video address.
  408.                MOV     AH,ATTRIBUTE            ;Retrieve attribute.
  409.                STOSW                           ;Put char/attrib in video buffer.
  410.                INC     DL                      ;Increment cursor column.
  411.                CMP     DL,BYTE PTR CRT_COLS    ;End of row?
  412.                JB      UPDATE_CURSOR           ;If no, update cursor.
  413.  
  414.                CMP     LINE_WRAP,OFF           ;Else, line wrap off?
  415.                JZ      FAST_END                ;If yes, don't move cursor.
  416.                XOR     DL,DL                   ;Else, column zero.
  417.                INC     DH                      ;Next row.
  418.                CALL    INFORMATION             ;Get displayable row info.
  419.                CMP     DH,AL                   ;Beyond the bottom of screen?
  420.                JBE     UPDATE_CURSOR           ;If no, update cursor.
  421.  
  422.                DEC     DH                      ;Else, cursor to original row.
  423.                MOV     DI,CRT_START            ;Point destination to top.
  424.                MOV     SI,DI                   ;Point source to second row
  425.                MOV     AX,CRT_COLS             ; by adding width in columns
  426.                PUSH    AX                      ; twice for char/attribute.
  427.                ADD     SI,AX
  428.                ADD     SI,AX
  429.                MUL     DH                      ;Times displayable rows - 1.
  430.                MOV     CX,AX                   ; equals char/attrib to scroll.
  431.                PUSH    DS                      ;Save data segment and
  432.                PUSH    ES                      ; point to video segment.
  433.                POP     DS
  434.                REP     MOVSW                   ;Scroll the screen.
  435.                POP     DS                      ;Restore data segment.
  436.                POP     CX                      ;Retrieve CRT columns.
  437.                MOV     AL,SPACE                ;Write space/attrib to
  438.                MOV     AH,ATTRIBUTE            ; bottom row.
  439.                REP     STOSW
  440.  
  441. UPDATE_CURSOR: CALL    SET_CURSOR              ;Update the cursor position.
  442. FAST_END:      POP     ES                      ;Restore extra segment.
  443.                RET
  444.  
  445. ;************* SUPPORT ROUTINES *************;
  446.  
  447. CK_QUOTE:      MOV     BX,OFFSET QUOTE_STATE   ;Assume quote state.
  448.                MOV     AH,DOUBLE_QUOTE
  449.                CMP     AL,AH                   ;Is it double quote?
  450.                JZ      GOT_QUOTE               ;If yes, string delimiter.
  451.                MOV     AH,SINGLE_QUOTE         ;Is it single quote?
  452.                CMP     AL,AH                   ;Is yes, string delimiter.
  453.                JNZ     QUOTE_END               ;Else, return ZR = 0.
  454. GOT_QUOTE:     MOV     QUOTE_TYPE,AH           ;Store as matching string end.
  455. QUOTE_END:     RET
  456.  
  457. ;------------------------------------------------;
  458. ACCUMULATE:    PUSH    AX                      ;Preserve number character.
  459.                SUB     AL,"0"                  ;Convert ASCII to binary.
  460.                MOV     CL,AL                   ;Save the number.
  461.                MOV     AX,10                   ;Multiply previous count by 10.
  462.                MOV     BX,NUMBER_COUNT
  463.                MUL     BYTE PTR NUMBER_BUFFER[BX]
  464.                ADD     AL,CL                            ;Add in new number
  465.                MOV     BYTE PTR NUMBER_BUFFER[BX],AL    ; and store.
  466.                POP     AX
  467.                RET
  468.  
  469. ;---------------------------------------------------------------------------;
  470. ; OUTPUT:  CY = 1 if write SLOW mode or in graphics mode; CY = 0 otherwise. ;
  471. ;---------------------------------------------------------------------------;
  472. CK_SLOW_TEXT:  TEST    STATUS,SLOW
  473.                JNZ     SLOW_MODE
  474.                CMP     CRT_MODE,7
  475.                JZ      TEXT_MODE
  476.                CMP     CRT_MODE,3
  477.                JA      SLOW_MODE
  478. TEXT_MODE:     CLC
  479.                RET
  480.  
  481. SLOW_MODE:     STC
  482.                RET
  483.  
  484. ;-------------------------------------;
  485. ; OUTPUT:  AL = Screen rows minus one ;
  486. ;-------------------------------------;
  487. INFORMATION:   PUSH    DS                      ;Save data segment.
  488.                MOV     AX,40                   ;Point to BIOS data.
  489.                MOV     DS,AX
  490.                MOV     AL,DS:[84H]             ;Retrieve rows - 1.
  491.                OR      AL,AL                   ;BIOS supported?
  492.                JNZ     INFO_END                ;If yes, done here.
  493.                MOV     AL,24                   ;Else, assume 25 lines.
  494. INFO_END:      POP     DS
  495.                RET
  496.  
  497. ;------------------------------------------------------------------------------;
  498. ; INPUT:  DX = Cursor position.                                                ;
  499. ; OUTPUT: ES = Video buffer segment; DI = Video buffer offset; AX,DX preserved ;
  500. ;------------------------------------------------------------------------------;
  501. VIDEO_SETUP:   PUSH    AX
  502.                MOV     AX,CRT_COLS             ;Retrieve CRT columns.
  503.                MUL     DH                      ;Times cursor row.
  504.                MOV     BL,DL
  505.                XOR     BH,BH
  506.                ADD     AX,BX                   ;Plus cursor column.
  507.                SHL     AX,1                    ;Times two for attribute.
  508.                MOV     DI,CRT_START            ;Plus starting video offset.
  509.                ADD     DI,AX                   ;Equals destination.
  510.  
  511.                MOV     BX,0B000H               ;Assume mono card.
  512.                CMP     ADDR_6845,3B4H          ;Is it mono port?
  513.                JZ      VIDEO_SEGMENT           ;If yes, guessed right.
  514.                ADD     BX,800H                 ;Else, point to color segment.
  515. VIDEO_SEGMENT: MOV     ES,BX
  516.                POP     AX
  517.                RET
  518.  
  519. ;------------------------------------------------;
  520. ; Move BIOS video data into our data segment.    ;
  521. ;------------------------------------------------;
  522. GET_BIOS_DATA: PUSH    DS
  523.                PUSH    DI                      ;Point to BIOS data segment.
  524.                MOV     BX,40H
  525.                MOV     DS,BX
  526.                MOV     SI,BIOS_ACTIVE_PAGE     ;Start with active page.
  527.                MOV     DI,OFFSET ACTIVE_PAGE
  528.                MOVSB                           ;Retrieve active page
  529.                MOVSW                           ; and address of 6845 port.
  530.                MOV     SI,BIOS_CRT_MODE        ;Retrieve CRT mode, CRT columns,
  531.                MOV     CX,CRT_DATA_LENGTH      ; CRT length, CRT start.
  532.                REP     MOVSB
  533.                MOV     BL,ES:ACTIVE_PAGE       ;Use active page as index
  534.                XOR     BH,BH                   ; of active cursor position.
  535.                SHL     BX,1
  536.                ADD     SI,BX
  537.                MOVSW
  538.                POP     DI
  539.                POP     DS
  540.                RET
  541.  
  542. ;----------------------------------------------------------------------------;
  543. ; OUTPUT: BL = First parameter; BH = Second parameter; DX = cursor position. ;
  544. ;----------------------------------------------------------------------------;
  545. ADJUST_NUMBER: MOV     BX,WORD PTR NUMBER_BUFFER
  546.                OR      BH,BH
  547.                JNZ     ADJUST_AL               ;If either parameter zero,
  548.                INC     BH                      ; increment to convert
  549. ADJUST_AL:     OR      BL,BL                   ; to base one.
  550.                JNZ     ADJUST_END
  551.                INC     BL
  552. ADJUST_END:    MOV     DX,CURSOR_POSN          ;Return cursor position.
  553.                RET
  554.  
  555. ;--------------------------------------------------------------------------;
  556. ; INPUT:  AX = number; BP = 0 if console output; BP = 1 if storage output. ;
  557. ;--------------------------------------------------------------------------;
  558. DECIMAL_OUT:   MOV     BX,10                   ;Divisor of ten.
  559.                XOR     CX,CX                   ;Zero in counter.
  560. NEXT_COUNT:    XOR     DX,DX                   ;Zero in high half.
  561.                DIV     BX                      ;Divide by ten.
  562.                ADD     DL,"0"                  ;Convert to ASCII.
  563.                PUSH    DX                      ;Save results.
  564.                INC     CX                      ;Also increment count.
  565.                CMP     AX,0                    ;Are we done?
  566.                JNZ     NEXT_COUNT              ;Continue until zero.
  567.                OR      BP,BP                   ;If BP zero, output to screen.
  568.                JNZ     DEVICE_OUTPUT           ;Else, store in buffer.
  569.  
  570. REPORT_OUTPUT: POP     AX                      ;Retrieve number.
  571.                CALL    PRINT_CHAR              ;Display it.
  572.                LOOP    REPORT_OUTPUT
  573.                RET
  574.  
  575. DEVICE_OUTPUT: POP     AX                      ;Retrieve number.
  576.                STOSB                           ;Store it
  577.                LOOP    DEVICE_OUTPUT
  578.                RET
  579.  
  580. ;************* ANSI COMMAND SUBSET *************;
  581.  
  582. CLS:           CALL    INFORMATION             ;Get displayable rows.
  583.                MOV     DH,AL                   ;Store in DH.
  584.                MOV     BH,7                    ;Assume normal attribute.
  585.                TEST    STATUS,OFF              ;Is ANSI OFF?
  586.                JNZ     CLS_SLOW                ;If yes, CLS via BIOS.
  587.                MOV     BH,ATTRIBUTE            ;Else, requested attribute.
  588.                CALL    CK_SLOW_TEXT
  589.                JC      CLS_SLOW                ;If ANSI SLOW, via BIOS.
  590.  
  591. CLS_FAST:      MOV     AH,BH                   ;Else, attribute in high half.
  592.                MOV     AL,SPACE                ;Space character in low half.
  593.                XOR     DX,DX                   ;Cursor position home.
  594.                CALL    VIDEO_SETUP             ;Calculate video address.
  595.                MOV     CX,CRT_LEN              ;Retrieve CRT length.
  596.                SHR     CX,1                    ;Times two for attribute.
  597.                REP     STOSW                   ;Write directly to video buffer.
  598.                JMP     SHORT SET_CURSOR        ;Set the cursor top left corner.
  599.  
  600. CLS_SLOW:      MOV     DL,BYTE PTR CRT_COLS    ;Retrieve CRT columns.
  601.                DEC     DL                      ;Adjust to zero base.
  602.                XOR     CX,CX                   ;Scroll active page.
  603.                MOV     AX,600H
  604.                INT     10H
  605.                XOR     DX,DX                   ;Home the cursor.
  606.                JMP     SHORT SET_CURSOR
  607.  
  608. ;------------------------------------------------;
  609. CURS_POSITION:                                 ;These two commands are the same.
  610. HORZ_VERT_POS: CALL    INFORMATION             ;Returns AL = screen rows.
  611.                CALL    ADJUST_NUMBER           ;Returns BL,BH = parameters.
  612.                SUB     BX,101H                 ;Zero based.
  613.                CMP     BL,AL                   ;If cursor request within
  614.                JA      CURSOR_END              ; boundaries of screen, set it.
  615.                CMP     BH,BYTE PTR CRT_COLS
  616.                JAE     CURSOR_END
  617.                XCHG    BH,BL
  618.                MOV     DX,BX
  619.  
  620. SET_CURSOR:    MOV     BH,ACTIVE_PAGE          ;Set cursor via BIOS.
  621.                MOV     AH,2
  622.                INT     10H
  623. CURSOR_END:    RET
  624.  
  625. ;------------------------------------------------;
  626. CURSOR_UP:     CALL    ADJUST_NUMBER           ;Move cursor up one or more rows
  627.                OR      DH,DH                   ; without changing column.
  628.                JZ      CURSOR_END              ;If already at top, ignore.
  629.                SUB     DH,BL
  630.                JNC     SET_CURSOR              ;Stay in bounds.
  631.                XOR     DH,DH
  632.                JMP     SHORT SET_CURSOR
  633.  
  634. ;------------------------------------------------;
  635. CURSOR_DOWN:   CALL    INFORMATION             ;Returns AL = screen rows.
  636.                CALL    ADJUST_NUMBER           ;Returns BL,BH =nos.; DX = cursor
  637.                CMP     DH,AL                   ;Move cursor down requested
  638.                JZ      CURSOR_END              ; rows without changing column.
  639.                ADD     DH,BL
  640.                CMP     DH,AL
  641.                JNA     SET_CURSOR
  642.                MOV     DH,AL                   ;Stay in bounds.
  643.                JMP     SHORT SET_CURSOR
  644.  
  645. ;------------------------------------------------;
  646. CURS_FORWARD:  CALL    ADJUST_NUMBER           ;Returns BL,BH =nos.; DX = cursor
  647.                MOV     BH,BYTE PTR CRT_COLS    ;Retrieve displayable columns.
  648.                DEC     BH                      ;Adjust to zero base.
  649.                CMP     DL,BH                   ;Move cursor forward one or more
  650.                JZ      CURSOR_END              ; columns without changing row.
  651.                ADD     DL,BL
  652.                CMP     DL,BH
  653.                JNA     SET_CURSOR
  654.                MOV     DL,BH                   ;Stay in bounds.
  655.                JMP     SHORT SET_CURSOR
  656.  
  657. ;------------------------------------------------;
  658. CURS_BACKWARD: CALL    ADJUST_NUMBER           ;Returns BL,BH =nos.; DX = cursor
  659.                OR      DL,DL                   ;Move cursor backward one or
  660.                JZ      CURSOR_END              ; more columns without changing
  661.                SUB     DL,BL                   ; row.  Ignore if already left.
  662.                JNC     SET_CURSOR
  663.                XOR     DL,DL                   ;Stay in bounds.
  664.                JMP     SHORT SET_CURSOR
  665.  
  666. ;--------------------------------------------------------;
  667. ; Report cursor position via console; Format: ESC[#;#R   ;
  668. ;--------------------------------------------------------;
  669. DEVICE_STATUS: MOV     DI,OFFSET DEVICE_STATUS_BUFFER
  670.                MOV     AL,ESC_CHAR
  671.                STOSB                           ;Store leading Esc, bracket
  672.                MOV     AL,"["                  ;in special Device Status Buffer.
  673.                STOSB
  674.  
  675.                MOV     AL,CURSOR_ROW           ;Retrieve cursor row.
  676.                XOR     AH,AH                   ;Zero in high half.
  677.                INC     AX                      ;Convert to base one.
  678.                MOV     BP,1                    ;Flag as storage.
  679.                CALL    DECIMAL_OUT             ;Convert to ASCII.
  680.                MOV     AL,";"                  ;Store delimiting semi-colon.
  681.                STOSB
  682.                MOV     AL,CURSOR_COL           ;Do same with cursor column.
  683.                XOR     AH,AH
  684.                INC     AX
  685.                CALL    DECIMAL_OUT
  686.                MOV     AL,"R"                  ;Add command character
  687.                STOSB
  688.                MOV     AL,CR                   ; and carriage return.
  689.                STOSB
  690.                SUB     DI,OFFSET DEVICE_STATUS_BUFFER   ;Setup for console out.
  691.                MOV     REASSIGN_COUNT,DI
  692.                MOV     REASSIGN_POS,OFFSET DEVICE_STATUS_BUFFER
  693.                MOV     REASSIGN_FLAG,ON
  694.                MOV     REMOVE_FLAG,OFF
  695.                RET
  696.  
  697. ;------------------------------------------------;
  698. SAVE_CURSOR:   MOV     DX,CURSOR_POSN
  699.                MOV     SAVE_POSITION,DX
  700.                RET
  701.  
  702. ;------------------------------------------------;
  703. RESTORE_CURS:  MOV     DX,SAVE_POSITION
  704.                JMP     SET_CURSOR
  705.  
  706. ;------------------------------------------------;
  707. ERASE_IN_LINE: MOV     DX,CURSOR_POSN          ;Erase from the cursor to
  708.                MOV     CX,CRT_COLS             ; the end of the line, including
  709.                SUB     CL,DL                   ; the current cursor position.
  710.                CALL    CK_SLOW_TEXT
  711.                JC      ERASE_SLOW              ;If ANSI ON and not graphics,
  712.                CALL    VIDEO_SETUP             ; write directly to video buffer
  713.                MOV     AL,SPACE                ; with space/attribute.
  714.                MOV     AH,ATTRIBUTE
  715.                REP     STOSW
  716.                RET
  717.  
  718. ERASE_SLOW:    MOV     CX,DX                   ;Else, erase SLOW via
  719.                MOV     DL,BYTE PTR CRT_COLS    ; BIOS scroll active page.
  720.                DEC     DL
  721.                MOV     BH,ATTRIBUTE
  722.                MOV     AX,601H
  723.                INT     10H
  724.                RET
  725.  
  726. ;------------------------------------------------;
  727. ;            Set Graphics Rendition              ;
  728. ;------------------------------------------------;
  729. SGR:           MOV     SI,OFFSET NUMBER_BUFFER     ;Point to number parameters.
  730. NEXT_SGR:      LODSB                               ;Retrieve parameter.
  731.                MOV     DI,OFFSET ATTRIBUTE_TABLE   ;Look up attribute in
  732.                MOV     CX,ATTRIBUTE_LENGTH         ; translation table.
  733.                REPNZ   SCASB
  734.                JNZ     SGR_LOOP
  735.  
  736.                MOV     DI,OFFSET ATTRIBUTE_END
  737.                SHL     CX,1
  738.                SUB     DI,CX
  739.                MOV     AX,[DI]
  740.                AND     ATTRIBUTE,AL            ;If match, AND with strip mask.
  741.                OR      ATTRIBUTE,AH            ;OR with add mask.
  742. SGR_LOOP:      DEC     NUMBER_COUNT            ;Do all parameters.
  743.                JNZ     NEXT_SGR
  744.                RET
  745.  
  746. ;------------------------------------------------;
  747. SET_MODE:      MOV     AH,ON                   ;Assume command 7,
  748.                JMP     SHORT CK_WRAP           ; turn line wrap on.
  749.  
  750. ;------------------------------------------------;
  751. RESET_MODE:    MOV     AH,OFF                     ;Assume turn line wrap off.
  752. CK_WRAP:       MOV     AL,BYTE PTR NUMBER_BUFFER  ;Retrieve number parameter.
  753.                CMP     AL,7                       ;Is it 7?
  754.                JZ      SET_WRAP                ;If yes, set wrap mode.
  755.                JB      DO_MODE                 ;1 - 6 valid modes.
  756.                CMP     AL,14                   ;14 - 16 valid modes.
  757.                JB      MODE_END
  758.                CMP     AL,19
  759.                JA      MODE_END                ;If above 16, illegal.
  760. DO_MODE:       XOR     AH,AH                   ;Else, set video mode
  761.                INT     10H                     ; to parameter.
  762.                RET
  763.  
  764. SET_WRAP:      MOV     LINE_WRAP,AH
  765. MODE_END:      RET
  766.  
  767. ;--------------------------------------------------------------------;
  768. ; Keyboard Key Reassignment:  First convert ASCII numbers to binary, ;
  769. ; parse out delimiting semi-colons, and quote string delimiters.     ;
  770. ;--------------------------------------------------------------------;
  771. REASSIGNMENT:  MOV     CX,ESC_COUNT            ;Retrieve length of Esc sequence.
  772.                DEC     CX                          ;Adjust.
  773.                MOV     SI,OFFSET ESC_BUFFER + 2    ;Point past Esc, bracket.
  774.                MOV     DI,OFFSET PARSE_BUFFER      ;Point to parse storage.
  775. NEXT_STRING:   MOV     QUOTE_TYPE,0                ;Reset quote type.
  776. NEXT_NUM:      XOR     DL,DL                   ;Use DL to carry number; init = 0
  777.                XOR     BP,BP                   ;Use BP as number flag.
  778. NEXT_PARAM:    LODSB                           ;Retrieve a byte.
  779.                DEC     CX                      ;Decrement string length counter.
  780.                JZ      PARSE_END               ;If zero, done.
  781.                MOV     AH,QUOTE_TYPE           ;Else, retrieve quote type.
  782.                OR      AH,AH                   ;If zero, not in string.
  783.                JNZ     QUOTE_MODE              ;Else, go to string mode.
  784.                CALL    CK_QUOTE                ;Is character a quote?
  785.                JZ      NEXT_PARAM              ;If yes, ignore.
  786.                CMP     AL,";"                  ;Else, is it semi-colon?
  787.                JZ      STORE_NUMBER            ;If yes, number delimiter.
  788.  
  789.                SUB     AL,"0"                  ;Else, must be number; convert
  790.                MOV     DH,AL                   ; to binary; save in DH.
  791.                MOV     AX,10                   ;Multiply current total by ten.
  792.                MUL     DL
  793.                ADD     AL,DH                   ;Add new number.
  794.                MOV     DL,AL                   ;Save in DL.
  795.                MOV     BP,1                    ;Flag that number mode active.
  796.                JMP     SHORT NEXT_PARAM        ;Next parameter.
  797.  
  798. STORE_NUMBER:  OR      BP,BP                   ;If number mode flag not set then
  799.                JZ      NEXT_PARAM              ;semi-colon not prefaced with no.
  800.                MOV     AL,DL                   ;Else, store number.
  801.                STOSB
  802.                JMP     SHORT NEXT_NUM          ;Reset number carrying registers.
  803.  
  804. QUOTE_MODE:    CMP     AL,AH                   ;Is current char a string ending
  805.                JZ      NEXT_STRING             ; quote?  If yes, exit quote mode
  806.                STOSB                           ;Else, store char as literal.
  807.                JMP     SHORT NEXT_PARAM
  808.  
  809. ;----------------------------------------------------------------------------;
  810. ; Remove duplicate assignments if removal leaves room for new assignment.    ;
  811. ; If no duplicate and room, add new assignment, else flush buffer to screen. ;
  812. ;----------------------------------------------------------------------------;
  813. PARSE_END:     OR      BP,BP                   ;Was last parameter a number?
  814.                JZ      CK_LENGTH               ;If no, skip.
  815.                MOV     AL,DL                   ;Else, store the last number.
  816.                STOSB
  817. CK_LENGTH:     SUB     DI,OFFSET PARSE_BUFFER - 2   ;Calculate string length
  818.                MOV     AX,DI                        ; including word for length.
  819.                MOV     BX,WORD PTR PARSE_BUFFER     ;BL=new first ASCII; BH=2nd.
  820.                MOV     CX,5                         ;String length has to be at
  821.                OR      BL,BL                   ;least word + 2 for old + new = 5
  822.                JZ      CK_LEGAL                ; for extended key reassignment.
  823.                DEC     CX                      ;And word + 1 for old + new = 4
  824. CK_LEGAL:      CMP     AX,CX                   ; for regular ASCII reassignment.
  825.                JB      ASSIGN_FLUSH            ;If not, illegal; flush buffer.
  826.                MOV     BP,BUFFER_END           ;BP to carry buffer end pointer.
  827.                CALL    CK_MATCH                ;Is char already reassigned?
  828.                JC      CK_ROOM                 ;If no, check room for new.
  829.  
  830. CK_REMOVE:     MOV     CX,DX                   ;REASSIGN_END - string end
  831.                SUB     CX,SI                   ; = bytes to move.
  832.                JZ      CK_ROOM                 ;If zero, last string.
  833.                SUB     DX,[DI]                 ;Is REASSIGN_END - old string
  834.                ADD     DX,AX                   ; + new string >
  835.                CMP     DX,BP                   ; BUFFER_END?
  836.                JA      ASSIGN_FLUSH            ;If yes, flush buffer to screen.
  837.                REP     MOVSB                   ;Else, move them down in buffer.
  838.                JMP     SHORT STORE_NEW
  839.  
  840. CK_ROOM:       ADD     DX,AX                   ;New string + current strings.
  841.                CMP     DX,BP                   ;Greater than buffer size?
  842.                JA      ASSIGN_FLUSH            ;If yes, flush new string.
  843. STORE_NEW:     MOV     SI,OFFSET PARSE_BUFFER  ;Else, room for new string.
  844.                STOSW                           ;Store string length first.
  845.                MOV     CX,AX                   ;Adjust counter.
  846.                DEC     CX
  847.                DEC     CX
  848.                REP     MOVSB                   ;Store the actual string.
  849.                MOV     REASSIGN_END,DI         ;Store new string end.
  850.                RET
  851.  
  852. ASSIGN_FLUSH:  MOV     AL,"p"                  ;If buffer full, flush string
  853.                JMP     FLUSH_BUFFER            ; including ending "p" command.
  854.  
  855. ;------------------------------------------------------------------------------;
  856. ; INPUT:  BL = first ASCII code to match; BH = extended if BL = 0              ;
  857. ; OUTPUT: CY = 1 if no match found; CY = 0 if match found.                     ;
  858. ;         DI points to string length of match; DI+2 points to start of string. ;
  859. ;         SI = end of string; DX = REASSIGN_END; BP = BUFFER_END               ;
  860. ;------------------------------------------------------------------------------;
  861. CK_MATCH:      MOV     DX,REASSIGN_END                ;Current strings end.
  862.                MOV     SI,OFFSET REASSIGNMENT_BUFFER  ;Point to buffer.
  863. NEXT_MATCH:    MOV     DI,SI                          ;Current record.
  864.                CMP     DI,DX                   ;End of strings?
  865.                JAE     NO_MATCH                ;If yes, no match.
  866.                MOV     CX,[DI+2]               ;CL=current first ASCII; CH=2nd
  867.                ADD     SI,[DI]                 ;Point to next record.
  868.                CMP     BL,CL                   ;First characters match?
  869.                JNZ     NEXT_MATCH              ;If no, check next record.
  870.                OR      BL,BL                   ;Extended code? ie zero.
  871.                JNZ     MATCH                   ;If no, then match.
  872.                CMP     BH,CH                   ;Else, check second code.
  873.                JNZ     NEXT_MATCH              ;If not same, no match.
  874. MATCH:         CLC                             ;Else return with CY = 0
  875.                RET
  876.  
  877. NO_MATCH:      STC                             ;No match; CY = 1.
  878.                RET
  879.  
  880. ;----------------------------------------------------------------------;
  881. ; Buffer area will write over disposable data and initialization code. ;
  882. ;----------------------------------------------------------------------;
  883. ESC_BUFFER            =       $
  884. NUMBER_BUFFER         =       ESC_BUFFER + ESC_BUFFER_SIZE
  885. PARSE_BUFFER          =       NUMBER_BUFFER
  886. DEVICE_STATUS_BUFFER  =       PARSE_BUFFER + ESC_BUFFER_SIZE
  887. DEVICE_STATUS_SIZE    =       11
  888. REASSIGNMENT_BUFFER   =       DEVICE_STATUS_BUFFER + DEVICE_STATUS_SIZE
  889.  
  890. ;              DISPOSABLE DATA
  891. ;              ---------------
  892.  
  893. SYNTAX         LABEL   BYTE
  894. DB      "Usage:  ANSI [FAST | SLOW][ON | OFF][/B nnn][/C][/U]",CR,LF
  895. DB      "FAST = direct screen writes; default",CR,LF
  896. DB      "SLOW = screen writes via BIOS",CR,LF
  897. DB      "ON/OFF = active/inactive; default is ON",CR,LF
  898. DB      "nnn = buffer size in bytes (0-60K) reserved for key reassignment; "
  899. DB      "default 200",CR,LF
  900. DB      "/U = Uninstall"
  901. CR_LF   DB     CR,LF,LF,"$"
  902.  
  903. STATUS_MSG     DB      "Status: $"
  904. BUFFER_MSG     DB      CR,LF,"Buffer size: $"
  905. BYTES_FREE     DB      CR,LF,"Bytes free:  $"
  906. ANSI_SYS_MSG   DB      "ANSI.SYS is installed so "
  907. NOT_INSTALLED  DB      "ANSI.COM not installed",CR,LF,"$"
  908. CON            DB      "CON"
  909. CON_OFFSET     EQU     10
  910.  
  911. SIZE_MSG       DB      "Uninstall to change buffer size",CR,LF,LF,"$"
  912. UNLOAD_MSG     DB      "ANSI can't be uninstalled",CR,LF
  913.                DB      "Uninstall resident programs in reverse order",CR,LF,"$"
  914. NOT_ENOUGH     DB      "Not enough memory",CR,LF,"$"
  915. ALLOCATE_MSG   DB      "Memory allocation error",CR,LF,BELL,"$"
  916. INSTALL_MSG    DB      "Installed",CR,LF,"$"
  917. UNINSTALL_MSG  DB      "Uninstalled",CR,LF,"$"
  918.  
  919. ;--------------------------------------------------------------------;
  920. ; Search memory for a copy of our code, to see if already installed. ;
  921. ;--------------------------------------------------------------------;
  922. INITIALIZE     PROC    NEAR
  923.                CLD                             ;All string operations forward.
  924.                MOV     BX,OFFSET START         ;Point to start of code.
  925.                NOT     BYTE PTR [BX]           ;Change a byte so no false match.
  926.                XOR     DX,DX                   ;Start at segment zero.
  927.                MOV     AX,CS                   ;Store our segment in AX.
  928. NEXT_PARAG:    INC     DX                      ;Next paragraph.
  929.                MOV     ES,DX
  930.                CMP     DX,AX                   ;Is it our segment?
  931.                JZ      ANNOUNCE                ;If yes, search is done.
  932.                MOV     SI,BX                   ;Else, point to our signature.
  933.                MOV     DI,BX                   ; and offset of possible match.
  934.                MOV     CX,16                   ;Check 16 bytes for match.
  935.                REP     CMPSB
  936.                JNZ     NEXT_PARAG              ;If no match, keep looking.
  937.  
  938. ;------------------------------------------------;
  939. ANNOUNCE:      MOV     DX,OFFSET SIGNATURE     ;Display our signature.
  940.                CALL    PRINT_STRING
  941.                MOV     DX,OFFSET SYNTAX        ;And syntax.
  942.                CALL    PRINT_STRING
  943.                MOV     SI,81H                  ;Point to command line.
  944. NEXT_CAP:      LODSB                           ;Capitalize parameters.
  945.                CMP     AL,CR
  946.                JZ      PARSE
  947.                CMP     AL,"a"
  948.                JB      NEXT_CAP
  949.                CMP     AL,"z"
  950.                JA      NEXT_CAP
  951.                AND     BYTE PTR [SI - 1],5FH
  952.                JMP     SHORT NEXT_CAP
  953.  
  954. ;------------------------------------------------;
  955. PARSE:         MOV     SI,81H                  ;Point to command line again.
  956. NEXT_PARA:     XOR     AX,AX                   ;Position in status parameters.
  957.                MOV     BX,4                    ;Status parameters each 4 bytes.
  958. NEXT_STATUS:   MOV     DI,OFFSET PARAMETERS    ;Point to "OFF ON  SLOWFAST"
  959.                ADD     DI,AX                   ;Point to next parameter.
  960.                ADD     AX,BX
  961.                CMP     AX,LAST_PARAMETER       ;Check all four possible statuses
  962.                JA      CK_SWITCHES
  963.                PUSH    SI                      ;Save command line pointer.
  964.                MOV     CX,2                    ;Check first two bytes for match.
  965.                REP     CMPSB
  966.                POP     SI                      ;Recover command line pointer.
  967.                JNZ     NEXT_STATUS
  968.  
  969.                DIV     BL                      ;If match, divide offset by four.
  970.                MOV     CL,1                    ;Set a bit to match offset.
  971. NEXT_SHIFT:    SHL     CL,1
  972.                DEC     AL
  973.                JNZ     NEXT_SHIFT
  974.                SHR     CL,1                    ;Adjust.
  975.                MOV     AH,STATUS_MASK          ;Retrieve appropriate ON/OFF
  976.                CMP     CL,ON                   ; or FAST/SLOW mask.
  977.                JBE     ADD_STATUS
  978.                ROL     AH,1
  979.                ROL     AH,1
  980.  
  981. ADD_STATUS:    AND     ES:STATUS,AH            ;Mask off old status.
  982.                OR      ES:STATUS,CL            ;Add new status.
  983.                INC     SI                      ;Bump command line pointer.
  984.                INC     SI
  985.  
  986. CK_SWITCHES:   LODSB                           ;Get a byte.
  987.                CMP     AL,CR                   ;Is it carriage return?
  988.                JZ      INSTALL                 ;If yes, done here.
  989.                CMP     AL,"/"                  ;Is there a switch character?
  990.                JNZ     NEXT_PARA               ;If no, keep looking.
  991.                LODSB                           ;Else, get the switch character.
  992.                CMP     AL,"B"                  ;Is it "B" ?
  993.                JNZ     CK_C                    ;If no, check "C".
  994.                CALL    CK_INSTALLED            ;Else, see if already installed.
  995.                JZ      GET_SIZE                ;If no, get buffer request size.
  996.                MOV     DX,OFFSET SIZE_MSG      ;Else, display error message.
  997.                CALL    PRINT_STRING
  998.                JMP     SHORT NEXT_PARA
  999. GET_SIZE:      CALL    DECIMAL_INPUT           ;Get number parameter.
  1000.                CMP     BX,REASSIGNMENT_MAX     ;If greater than maximum, use
  1001.                JBE     STORE_BUFFER            ; maximum, else use requested
  1002.                MOV     BX,REASSIGNMENT_MAX     ; size.
  1003. STORE_BUFFER:  MOV     REASSIGNMENT_SIZE,BX
  1004.                ADD     BX,OFFSET REASSIGNMENT_BUFFER  ;Calculate end of buffer.
  1005.                MOV     BUFFER_END,BX
  1006.                JMP     SHORT NEXT_PARA
  1007.  
  1008. CK_C:          CMP     AL,"C"                  ;Is it "C" ?
  1009.                JNZ     CK_U                    ;If not, check "U".
  1010.                MOV     ES:REASSIGN_END,OFFSET REASSIGNMENT_BUFFER  ;Clear buffer
  1011. CK_U:          CMP     AL,"U"                  ;Is it "U" ?
  1012.                JZ      DO_U                    ;If yes, try to uninstall.
  1013.                JMP     NEXT_PARA               ;Else, next parmater.
  1014. DO_U:          CALL    CK_INSTALLED            ;Else, see if installed.
  1015.                MOV     DX,OFFSET NOT_INSTALLED ;If no, exit with error message.
  1016.                JZ      LILLY_PAD               ;Too far for short jump.
  1017.                JMP     UNINSTALL               ;Else, uninstall.
  1018.  
  1019. ;------------------------------------------------;
  1020. INSTALL:       CALL    STATUS_REPORT           ;Display status.
  1021.                CALL    CK_INSTALLED            ;Check if already installed.
  1022.                JZ      CK_AVAILABLE            ;If no, see if enough memory.
  1023.                OR      AL,AL                   ;Else, done.
  1024.                JMP     EXIT                    ;Exit with ERRORLEVEL = 0.
  1025.  
  1026. ;--------------------------------;
  1027. ; This is the install procedure. ;
  1028. ;--------------------------------;
  1029. CK_AVAILABLE:  MOV     BP,OFFSET REASSIGNMENT_BUFFER   ;TSR ends at end
  1030.                ADD     BP,REASSIGNMENT_SIZE            ; of reassignment buffer.
  1031.                ADD     BP,15                           ;Round up.
  1032.                CMP     BP,DS:[6]               ;Buffer > PSP bytes in segment?
  1033.                MOV     DX,OFFSET NOT_ENOUGH    ;If yes, exit without installing
  1034.                JA      MSG_EXIT                ; with message.
  1035.  
  1036.                MOV     AX,3529H                ;Get undocumented INT 29 vector.
  1037.                INT     21H
  1038.                MOV     SI,OFFSET CON           ;Does it point to ANSI.SYS?
  1039.                MOV     DI,CON_OFFSET           ;Check by looking for "CON"
  1040.                MOV     CX,3                    ; as device name.
  1041.                REP     CMPSB
  1042.                MOV     DX,OFFSET ANSI_SYS_MSG  ;Exit with error message if
  1043. LILLY_PAD:     JZ      MSG_EXIT                ;ANSI.SYS loaded.
  1044.  
  1045.                MOV     OLD_INT_29[0],BX        ;Else save old interrupt.
  1046.                MOV     OLD_INT_29[2],ES
  1047.                MOV     DX,OFFSET ANSI_INT_29   ;Install new interrupt.
  1048.                MOV     AX,2529H
  1049.                INT     21H
  1050.                MOV     AX,3516H                ;Get INT 16 vector.
  1051.                INT     21H
  1052.                MOV     OLD_INT_16[0],BX        ;Save old interrupt.
  1053.                MOV     OLD_INT_16[2],ES
  1054.                MOV     DX,OFFSET ANSI_INT_16   ;Install new interrupt.
  1055.                MOV     AX,2516H
  1056.                INT     21H
  1057.                MOV     AX,3521H                ;Get DOS 21h interrupt.
  1058.                INT     21H
  1059.                MOV     OLD_INT_21[0],BX        ;Save old interrupt.
  1060.                MOV     OLD_INT_21[2],ES
  1061.                MOV     DX,OFFSET ANSI_INT_21   ;Install new interrupt.
  1062.                MOV     AX,2521H
  1063.                INT     21H
  1064.  
  1065.                MOV     AX,DS:[2CH]             ;Get environment segment.
  1066.                MOV     ES,AX
  1067.                MOV     AH,49H                  ;Free up environment.
  1068.                INT     21H
  1069.  
  1070.                MOV     DX,OFFSET INSTALL_MSG   ;Display install message.
  1071.                CALL    PRINT_STRING
  1072.                CALL    FLUSH_END               ;Setup the number buffer.
  1073.                MOV     DX,BP                   ;Retrieve resident byte request.
  1074.                MOV     CL,4
  1075.                SHR     DX,CL                   ;Convert to paragraphs.
  1076.                MOV     AX,3100H                ;Return error code of zero.
  1077.                INT     21H                     ;Terminate but stay resident.
  1078.  
  1079. ;-------------------------------------------------------------------;
  1080. ; Exit.  Return ERRORLEVEL code 0 if successful, 1 if unsuccessful. ;
  1081. ;-------------------------------------------------------------------;
  1082. MSG_EXIT:      CALL    PRINT_STRING
  1083. ERROR_EXIT:    MOV     AL,1                    ;ERRORLEVEL = 1.
  1084. EXIT:          MOV     AH,4CH                  ;Terminate.
  1085.                INT     21H
  1086.  
  1087. ;---------------------------------------------------;
  1088. ; This subroutine uninstalls the resident ANSI.COM. ;
  1089. ;---------------------------------------------------;
  1090. UNINSTALL:     AND     ES:STATUS,NOT ON        ;Turn OFF ANSI, just in case
  1091.                OR      ES:STATUS,OFF           ; can't uninstall.
  1092.                MOV     CX,ES                   ;Save segment in CX.
  1093.                MOV     AX,3529H                ;Get interrupt 29h.
  1094.                INT     21H
  1095.                CMP     BX,OFFSET ANSI_INT_29   ;Has it been hooked by another?
  1096.                JNZ     UNINSTALL_ERR           ;If yes, exit with error message.
  1097.                MOV     BX,ES
  1098.                CMP     BX,CX                   ;Is the segment vector same?
  1099.                JNZ     UNINSTALL_ERR           ;If no, exit with error message.
  1100.  
  1101.                MOV     AX,3516H                ;Get interrupt 16h.
  1102.                INT     21H
  1103.                CMP     BX,OFFSET ANSI_INT_16   ;Has it been hooked by another?
  1104.                JNZ     UNINSTALL_ERR           ;If yes, exit with error message.
  1105.                MOV     BX,ES
  1106.                CMP     BX,CX                   ;Is the segment vector same?
  1107.                JNZ     UNINSTALL_ERR           ;If no, exit with error message.
  1108.  
  1109.                MOV     AX,3521H                ;Get interrupt 21h.
  1110.                INT     21H
  1111.                CMP     BX,OFFSET ANSI_INT_21   ;Has it been hooked by another?
  1112.                JNZ     UNINSTALL_ERR           ;If yes, exit with error message.
  1113.                MOV     BX,ES
  1114.                CMP     BX,CX                   ;Is the segment vector same?
  1115.                JNZ     UNINSTALL_ERR           ;If no, exit with error message.
  1116.  
  1117.                MOV     AH,49H                  ;Return memory to system pool.
  1118.                INT     21H
  1119.                MOV     DX,OFFSET ALLOCATE_MSG
  1120.                JC      MSG_EXIT                ;Display message if problem.
  1121.  
  1122.                MOV     DX,ES:OLD_INT_29[0]     ;Restore old INT 29.
  1123.                MOV     DS,ES:OLD_INT_29[2]
  1124.                MOV     AX,2529H
  1125.                INT     21H
  1126.                MOV     DX,ES:OLD_INT_16[0]     ;Restore old INT 16.
  1127.                MOV     DS,ES:OLD_INT_16[2]
  1128.                MOV     AX,2516H
  1129.                INT     21H
  1130.                MOV     DX,ES:OLD_INT_21[0]     ;Restore old INT 21.
  1131.                MOV     DS,ES:OLD_INT_21[2]
  1132.                MOV     AX,2521H
  1133.                INT     21H
  1134.  
  1135.                PUSH    CS
  1136.                POP     DS                      ;Point to our data.
  1137.                MOV     DX,OFFSET UNINSTALL_MSG ;Display uninstall message.
  1138.                CALL    PRINT_STRING
  1139.                OR      AL,AL                   ;Exit with ERRORLEVEL = 0.
  1140.                JMP     EXIT
  1141.  
  1142. UNINSTALL_ERR: MOV     ES,CX                   ;If error, display OFF status.
  1143.                CALL    STATUS_REPORT
  1144.                MOV     DX,OFFSET UNLOAD_MSG    ;And exit with error message.
  1145.                JMP     MSG_EXIT
  1146. INITIALIZE     ENDP
  1147.  
  1148. ;--------------------------------------------------;
  1149. ; INPUT:  SI points to parameter start.            ;
  1150. ; OUTPUT: SI points to parameter end; BX = number. ;
  1151. ;--------------------------------------------------;
  1152. DECIMAL_INPUT  PROC    NEAR
  1153.                XOR     BX,BX                   ;Start with zero as number.
  1154. NEXT_DECIMAL:  LODSB                           ;Get a character.
  1155.                CMP     AL,CR                   ;Carriage return?
  1156.                JZ      ADJUST_DEC              ;If yes, done here.
  1157.                CMP     AL,"/"                  ;Forward slash?
  1158.                JZ      ADJUST_DEC              ;If yes, done here.
  1159.                SUB     AL,"0"                  ;ASCII to binary.
  1160.                JC      NEXT_DECIMAL            ;If not between 0 and 9, skip.
  1161.                CMP     AL,9
  1162.                JA      NEXT_DECIMAL
  1163.                CBW                             ;Convert byte to word.
  1164.                XCHG    AX,BX                   ;Swap old and new number.
  1165.                MOV     CX,10                   ;Shift to left by multiplying
  1166.                MUL     CX                      ; last entry by ten.
  1167.                JC      DECIMAL_ERROR           ;If carry, too big.
  1168.                ADD     BX,AX                   ;Add new number and store in BX.
  1169.                JNC     NEXT_DECIMAL            ;If not carry, next number.
  1170. DECIMAL_ERROR: MOV     BX,-1                   ;Else, too big; return -1.
  1171.  
  1172. ADJUST_DEC:    DEC     SI                      ;Adjust pointer.
  1173.                RET
  1174. DECIMAL_INPUT  ENDP
  1175.  
  1176. ;-------------------------------------------------------;
  1177. ; OUTPUT: ZR = 1 if not installed; ZR = 0 if installed. ;
  1178. ;-------------------------------------------------------;
  1179. CK_INSTALLED:  MOV     AX,ES
  1180.                MOV     BX,CS
  1181.                CMP     AX,BX                   ;Compare segments.
  1182.                RET
  1183.  
  1184. ;------------------------------------------------;
  1185. STATUS_REPORT: MOV     DX,OFFSET STATUS_MSG
  1186.                CALL    PRINT_STRING            ;Display "Status: ".
  1187.                MOV     BL,ES:STATUS
  1188.                MOV     BH,1
  1189. NEXT_REPORT:   TEST    BL,BH
  1190.                JZ      LOOP_STATUS
  1191.  
  1192.                MOV     DL,BH
  1193.                MOV     AX,-1
  1194. NEXT_BIT:      INC     AX
  1195.                SHR     DL,1
  1196.                JNC     NEXT_BIT
  1197.                SHL     AX,1
  1198.                SHL     AX,1
  1199.                MOV     SI,OFFSET PARAMETERS    ;Display appropriate ON or OFF,
  1200.                ADD     SI,AX                   ; SLOW or FAST.
  1201.                MOV     CX,4
  1202. REPORT:        LODSB
  1203.                CALL    PRINT_CHAR
  1204.                LOOP    REPORT
  1205.  
  1206. LOOP_STATUS:   SHL     BH,1
  1207.                CMP     BH,FAST
  1208.                JBE     NEXT_REPORT
  1209.                MOV     DX,OFFSET BUFFER_MSG    ;Display "Buffer size: ".
  1210.                CALL    PRINT_STRING
  1211.                MOV     AX,ES:REASSIGNMENT_SIZE
  1212.                PUSH    AX
  1213.                XOR     BP,BP
  1214.                CALL    DECIMAL_OUT             ;Display the size.
  1215.                MOV     DX,OFFSET BYTES_FREE    ;Display "Bytes free: ".
  1216.                CALL    PRINT_STRING
  1217.                POP     AX
  1218.                ADD     AX,OFFSET REASSIGNMENT_BUFFER
  1219.                SUB     AX,ES:REASSIGN_END
  1220.                CALL    DECIMAL_OUT             ;Display the bytes free.
  1221.                MOV     DX,OFFSET CR_LF
  1222.                CALL    PRINT_STRING
  1223.                RET
  1224.  
  1225. ;------------------------------------------------;
  1226. PRINT_CHAR:    MOV     DL,AL
  1227.                MOV     AH,2                    ;Print character via DOS.
  1228.                JMP     SHORT DOS_INT
  1229.  
  1230. PRINT_STRING:  MOV     AH,9                    ;Print string via DOS.
  1231. DOS_INT:       INT     21H
  1232.                RET
  1233.  
  1234. _TEXT          ENDS
  1235.                END     START
  1236.